﻿using Covid.Server.Data;
using Covid.Server.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Covid.Server.Services
{
    public class DailyHealthcsRepository : IDailyHealthcsRepository
    {

        private readonly MyDbContext _context;
        /// <summary>
        /// 通过构造函数注入dbContext
        /// </summary>
        public DailyHealthcsRepository(MyDbContext context)
        {
            _context = context;
        }


        /// <summary>
        /// 根据部门ID和时间获取部门当天所有员工的健康状态
        /// </summary>
        /// <param name="departmentId"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public async Task<IList<DailyHealth>> GetByDepartmentAsync(int departmentId, DateTime date)
        {
            var employeeIds = await _context.Employees.Where(u => u.DepartmentId.Equals(departmentId)).Select(u => u.Id).ToListAsync();
            var dailHealths = await _context.DailyHealths.Where(u => u.Date.Equals(date) && employeeIds.Contains(u.EmployeeId)).ToListAsync();
            return dailHealths;
        }


        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="departmentId"></param>
        /// <param name="date"></param>
        /// <param name="dailyHealthcs"></param>
        /// <returns></returns>
        public async Task UpdateForDepartmentAsync(int departmentId, DateTime date, IList<DailyHealth> dailyHealths)
        {
            //根据部门ID获取所有的部门员工，查询出所有的员工ID
            var employeeIds = await _context.Employees
                .Where(x => x.DepartmentId == departmentId)
                .Select(x => x.Id)
                .ToListAsync();
            //根据员工ID 和 时间 查询出健康记录
            var inDb = await _context.DailyHealths
                .Where(x => x.Date == date && employeeIds.Contains(x.EmployeeId))
                .ToListAsync();

            foreach (var dbItem in inDb)
            {
                var one = dailyHealths
                    .SingleOrDefault(x =>
                        x.EmployeeId == dbItem.EmployeeId && x.Date == dbItem.Date);
                if (one != null)
                {
                    dbItem.HealthCondition = one.HealthCondition;
                    dbItem.Temperature = one.Temperature;
                    dbItem.Remark = one.Remark;
                    _context.Update(dbItem);
                }
            }

            var dbKeys = inDb.Select(x =>
                new { x.EmployeeId, x.Date }).ToList();
            var incomingKeys = dailyHealths
                .Select(x => new { x.EmployeeId, x.Date }).ToList();
            var toAddKeys = incomingKeys.Except(dbKeys); // todo
            foreach (var addKey in toAddKeys)
            {
                var toAdd = dailyHealths
                    .Single(x => x.EmployeeId == addKey.EmployeeId
                                 && x.Date == addKey.Date);
                await _context.AddAsync(toAdd);
            }

            var toRemoveKeys = dbKeys.Except(incomingKeys); // todo
            foreach (var removeKey in toRemoveKeys)
            {
                var toRemove = inDb
                    .Single(x => x.EmployeeId == removeKey.EmployeeId
                                 && x.Date == removeKey.Date);
                _context.Remove(toRemove);
            }

            await _context.SaveChangesAsync();
        }


    }
}
